<!--
 * @Description: 历史数据
 * @Author: Ronda
 * @Date: 2022-02-16 09:30:08
 * @LastEditors: 王裕生 2329843886@qq.com
 * @LastEditTime: 2025-09-25 10:43:18
-->
<template>
    <div>
        <el-row>
            <el-col :span="4">
                <div class="tree">
                    <el-input placeholder="输入关键字进行过滤" v-model="filterText">
                    </el-input>

                    <el-tree @node-click="handlerClick" class="filter-tree" :data="EngData" :props="defaultProps"
                        default-expand-all :filter-node-method="filterNode" ref="tree">
                    </el-tree>
                </div>
            </el-col>
            <el-col :span="20">
                <div v-if="params.device_id" class="table">
                    <el-form :inline="true" :model="form">
                        <!-- 时间日期选择器 -->
                        <el-form-item>
                            <el-date-picker value-format="timestamp" v-model="form.time" type="datetimerange"
                                :picker-options="pickerOptions" range-separator="至" start-placeholder="开始日期"
                                end-placeholder="结束日期" align="right">
                            </el-date-picker>
                        </el-form-item>
                        <!--  -->
                        <el-form-item>
                            <el-button type="primary" @click="toSearch">搜索</el-button>
                        </el-form-item>
                    </el-form>
                    <!-- 表格 -->
                    <el-table :data="monitorData" style="width: 100%" :row-class-name="tableRowClassName">
                        <el-table-column prop="insert_time" label="日期" width="180" align="center">
                            <template v-slot="scope">
                                <div>
                                    {{ scope.row.insert_time | fmtDate }}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="tsp" label="tsp" align="center">
                        </el-table-column>
                        <el-table-column prop="pm10" label="pm10" align="center">
                        </el-table-column>
                        <el-table-column prop="pm25" label="pm2.5" align="center">
                        </el-table-column>
                        <el-table-column prop="temperature" label="温度" align="center">
                        </el-table-column>
                        <el-table-column prop="humidity" label="湿度" align="center">
                        </el-table-column>
                        <el-table-column prop="wind_direction" label="风向" align="center">
                        </el-table-column>
                        <el-table-column prop="wind_speed" label="风速" align="center">
                        </el-table-column>
                    </el-table>
                </div>
                <div v-else style="line-height: 50px;color:red;font-size: 28px;text-align: center;">请点击设备查看</div>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import { get } from '@/utils/request'
export default {
    data() {
        return {
            filterText: '',
            defaultProps: {
                children: 'children',
                label: 'name'
            },

            // 表格数据源
            monitorData: [],
            form: { time: [] },
            // 树形结构展示数据
            EngData: [],
            // 快捷选项配置项
            pickerOptions: {
                shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
            params: {
                page: 1,
                pageSize: 10,
                device_id: 0,
                engineer_id: 0
            }
        }
    },
    watch: {
        // 监听用户输入框输入的值并进行过滤
        filterText(val) {
            this.$refs.tree.filter(val);
        }
    },
    methods: {
        // 点击节点触发
        handlerClick(val, node) {
            // 判断点击的是设备 将工程id 设备id 传给后端
            if (!val.children) {
                this.params.device_id = val.id;
                this.params.engineer_id = val.engineer_id
                this.getMonitor();
            }
        },
        // 搜索
        toSearch() {
            this.params.page = 1;
            this.getMonitor();
        },
        // 封装方法获取监测历史数据
        async getMonitor() {
            let temp = {
                ...this.params
            };
            if (this.form.time) {
                temp.begin_time = this.form.time[0],
                    temp.end_time = this.form.time[1]
            }
            let res = await get('/monitor/pageQuery', temp);
            console.log(res, '-------');
            this.monitorData = res.data.list;
        },
        // 封装方法获取工程设备树
        async getEngDevData() {
            let res = await get('/engineer/findEngineerDeviceTree');
            console.log(res, '88888');
            this.EngData = res.data;
        },
        filterNode(value, data) {
            // data是树形结构数据
            if (!value) return true;
            // value输入框输入的值
            // 返回就是包含输入框关键字的label(节点)
            return data.name.indexOf(value) !== -1;
        },
        tableRowClassName({ row, rowIndex }) {
            if (rowIndex % 2 == 0 || rowIndex == 0) {
                return 'warning-row';
            } else {
                return 'success-row';
            }
            return '';
        }
    },
    created() {
        this.getEngDevData();
        this.getMonitor();
    }
}
</script>
<style lang="scss" scoped>
.tree {
    height: calc(100vh - 60px);
    padding-right: 10px;
    border-right: 1px solid #ececec;
    margin-right: 10px;
}

::v-deep .el-table .warning-row {
    background: rgb(182, 211, 224);
}

::v-deep .el-table .success-row {
    background: rgb(185, 225, 236);
}
</style>